package gth.news.service;

import gth.news.dao.NewsRepository;
import gth.news.domain.News;
import gth.news.dto.NewsDto;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class NewsService {

    private NewsRepository newsRepository;

    /**
     *
     * @return
     */
    public List<NewsDto> findAll() throws PropertyVetoException, SQLException {
        newsRepository = new NewsRepository();
        List<News> list = newsRepository.findAll();
        return list.stream().map(NewsDto::toDto).collect(Collectors.toList());
    }

    /**
     *
     * @param id
     * @return
     */
    public NewsDto findById(Integer id) throws PropertyVetoException, SQLException {
        newsRepository = new NewsRepository();
        News news= newsRepository.findById(id);
        return NewsDto.toDto(news);
    }
}
